CC = gcc
CFLAGS = -Wall -Wextra -std=c11 -fPIC
LDFLAGS = -pthread
AR = ar
ARFLAGS = rcs

# 目录定义
SRC_DIR = src
COMMON_DIR = $(SRC_DIR)/common
SERVER_DIR = $(SRC_DIR)/server
VIEWER_DIR = $(SRC_DIR)/viewer
TESTS_DIR = tests
BUILD_DIR = build
LIB_DIR = lib
BIN_DIR = bin

# 目标文件
SHARED_LIB = $(LIB_DIR)/libclog.so
STATIC_LIB = $(LIB_DIR)/libclog.a
SERVER_BIN = $(BIN_DIR)/clog_server
VIEWER_BIN = $(BIN_DIR)/clog_viewer
TEST1_BIN = $(BIN_DIR)/test_app1
TEST2_BIN = $(BIN_DIR)/test_app2
DEMO_BIN = $(BIN_DIR)/demo_program
TEST_PATH_BIN = $(BIN_DIR)/test_absolute_path

# 源文件
COMMON_SRCS = $(COMMON_DIR)/clog.c $(COMMON_DIR)/config.c $(COMMON_DIR)/common.c
SERVER_SRCS = $(SERVER_DIR)/log_server.c
VIEWER_SRCS = $(VIEWER_DIR)/log_viewer.c
TEST1_SRCS = $(TESTS_DIR)/test_app1.c
TEST2_SRCS = $(TESTS_DIR)/test_app2.c

# 对象文件
COMMON_OBJS = $(BUILD_DIR)/clog.o $(BUILD_DIR)/config.o $(BUILD_DIR)/common.o
SERVER_OBJS = $(BUILD_DIR)/log_server.o $(BUILD_DIR)/config.o $(BUILD_DIR)/common.o
VIEWER_OBJS = $(BUILD_DIR)/log_viewer.o $(BUILD_DIR)/config.o $(BUILD_DIR)/common.o

.PHONY: all clean install shared static server viewer tests setup directories

all: directories shared static server viewer tests

# 创建目录
directories:
	@mkdir -p $(BUILD_DIR) $(LIB_DIR) $(BIN_DIR)

# 编译共享库
shared: directories $(SHARED_LIB)

$(SHARED_LIB): $(COMMON_OBJS)
	$(CC) -shared -o $@ $^ $(LDFLAGS)
	@echo "Built shared library: $@"

# 编译静态库
static: directories $(STATIC_LIB)

$(STATIC_LIB): $(COMMON_OBJS)
	$(AR) $(ARFLAGS) $@ $^
	@echo "Built static library: $@"

# 编译对象文件
$(BUILD_DIR)/clog.o: $(COMMON_DIR)/clog.c $(COMMON_DIR)/clog.h $(COMMON_DIR)/config.h $(COMMON_DIR)/common.h
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/config.o: $(COMMON_DIR)/config.c $(COMMON_DIR)/config.h $(COMMON_DIR)/clog.h
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/common.o: $(COMMON_DIR)/common.c $(COMMON_DIR)/common.h $(COMMON_DIR)/config.h $(COMMON_DIR)/clog.h
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/log_server.o: $(SERVER_DIR)/log_server.c $(COMMON_DIR)/clog.h $(COMMON_DIR)/config.h $(COMMON_DIR)/common.h
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/log_viewer.o: $(VIEWER_DIR)/log_viewer.c $(COMMON_DIR)/clog.h $(COMMON_DIR)/config.h $(COMMON_DIR)/common.h
	$(CC) $(CFLAGS) -c $< -o $@

# 编译服务器
server: directories $(SERVER_BIN)

$(SERVER_BIN): $(SERVER_OBJS)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
	@echo "Built server: $@"

# 编译查看器
viewer: directories $(VIEWER_BIN)

$(VIEWER_BIN): $(VIEWER_OBJS)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
	@echo "Built viewer: $@"

# 编译测试程序
tests: directories shared $(TEST1_BIN) $(TEST2_BIN) $(DEMO_BIN) $(TEST_PATH_BIN)

$(TEST1_BIN): $(TEST1_SRCS) $(SHARED_LIB)
	$(CC) $(CFLAGS) -I$(COMMON_DIR) -o $@ $< -L$(LIB_DIR) -lclog $(LDFLAGS)
	@echo "Built test1: $@"

$(TEST2_BIN): $(TEST2_SRCS) $(SHARED_LIB)
	$(CC) $(CFLAGS) -I$(COMMON_DIR) -o $@ $< -L$(LIB_DIR) -lclog $(LDFLAGS)
	@echo "Built test2: $@"

$(DEMO_BIN): demo_program.c $(SHARED_LIB)
	$(CC) $(CFLAGS) -I. -o $@ $< -L$(LIB_DIR) -lclog $(LDFLAGS)
	@echo "Built demo program: $@"

$(TEST_PATH_BIN): test_absolute_path.c $(STATIC_LIB)
	$(CC) $(CFLAGS) -I. -o $@ $< $(BUILD_DIR)/config.o $(LDFLAGS)
	@echo "Built path test: $@"

# 安装
install: all
	@echo "Installing libclog..."
	sudo cp $(SHARED_LIB) /usr/local/lib/
	sudo cp $(STATIC_LIB) /usr/local/lib/
	sudo cp $(COMMON_DIR)/clog.h /usr/local/include/
	sudo ldconfig
	@echo "Installation complete"

# 创建日志目录
setup:
	mkdir -p logs

# 运行服务器
run-server: server setup
	./$(SERVER_BIN)

# 运行查看器（显示所有日志）
run-viewer: viewer
	./$(VIEWER_BIN)

# 运行查看器（只显示test_app1的日志）
run-viewer-test1: viewer
	./$(VIEWER_BIN) -p test_app1

# 运行查看器（只显示错误日志）
run-viewer-error: viewer
	./$(VIEWER_BIN) -l error

# 运行演示程序
run-demo: $(DEMO_BIN)
	LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH ./$(DEMO_BIN)

# 测试绝对路径功能
run-test-path: $(TEST_PATH_BIN)
	./$(TEST_PATH_BIN)

# 运行测试程序1
run-test1: $(TEST1_BIN)
	LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH ./$(TEST1_BIN)

# 运行测试程序2
run-test2: $(TEST2_BIN)
	LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH ./$(TEST2_BIN)

# 运行所有测试
test: tests
	@echo "Starting both test applications..."
	LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH ./$(TEST1_BIN) &
	LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH ./$(TEST2_BIN) &
	@echo "Test applications started. Check logs with 'make run-viewer'"

# 演示
demo: all setup
	@echo "Starting demo..."
	./$(SERVER_BIN) &
	sleep 2
	LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH ./$(TEST1_BIN) &
	LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH ./$(TEST2_BIN) &
	sleep 5
	@echo "Demo completed. Check logs in ./logs/"

# 清理
clean:
	rm -rf $(BUILD_DIR) $(LIB_DIR) $(BIN_DIR)
	rm -rf logs
	rm -f /tmp/log_server.sock /tmp/test_log_server.sock
	rm -rf /tmp/clog_test_logs /tmp/test_clog_absolute /tmp/test_clog_nested

# 卸载
uninstall:
	sudo rm -f /usr/local/lib/libclog.so /usr/local/lib/libclog.a
	sudo rm -f /usr/local/include/clog.h
	sudo ldconfig
